$(document).ready(function () {
    userChange();
    var id = UrlParm.parm("id");
    showInfo(id);
    $(".buy").on("click",function () {
        buy(id);
    })
})
/*展示商品信息*/
function showInfo(id){
    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"findById","id":id},
        "dataType":"json",
        "success":function(result) {
            $(".picture").append(showPicture(result));
            $(".descriptionbox").append(showDescription(result));
            $(".namebox").append(showName(result));
            $(".pricebox").append(showPrice(result));
            addColor(result.name);
        }
    })
}
function  showPicture(goods) {
    return $("<img src='"+goods.picture+"'>");
}
function showName(goods) {
    return $("<p class='name'>"+goods.name+"</p>");
}
function  showPrice(goods) {
    return $("<p class='price'>价格："+goods.price+"</p>");
}
function  showDescription(goods) {
    return $("<div><p>"+goods.description+"</p></div>")
}

/*添加规格选项*/
function addColor(goodsName) {
    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"getDetailInfo","fieldName":"color","goodsName":goodsName},
        "dataType":"json",
        "success":function (result) {
            var arry  = eval(result);
            for(var i=0;i<arry.length;i++){
                $(".color").append(showColor(arry[i],i));
            }
            addCapacity(goodsName);
        }
    })
}
function showColor(color,i) {
    return $("<input class='color"+i+"' type='button' value='"+color+"'>");
}
/*展示容量*/
function addCapacity(goodsName) {
    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"getDetailInfo","fieldName":"capacity","goodsName":goodsName},
        "dataType":"json",
        "success":function (result) {
            var arry  = eval(result);
            for(var i=0;i<arry.length;i++){
                $(".capacity").append(showCapacity(arry[i]),i);
            }
            addNetworkType(goodsName);
        }
    })
}
function showCapacity(capacity,i) {
    return $("<input class='capacity"+i+"' type='button' value='"+capacity+"'>");
}
/*展示网络模式*/
function addNetworkType(goodsName) {
    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"getDetailInfo","fieldName":"network_type","goodsName":goodsName},
        "dataType":"json",
        "success":function (result) {
            var arry  = eval(result);
            for(var i=0;i<arry.length;i++){
                $(".network_type").append(shownetworkType(arry[i],i));
            }
        }
    })
}
function shownetworkType(networkType,i) {
    return $("<input class='networkType"+i+"' type='button' value='"+networkType+"'>");
}
function buy(id) {
    $.ajax({
        "url":"../shopcar.do",
        "type":"POST",
        "data":{"method":"addInfo","goodsId":id},
        "dataType":"text",
        "success":function (result) {
            if(result=='true'){
                alert("添加购物车成功！");
            }else  if(result=='flse'){
                alert("添加失败！");
            }else if(result=='nologin'){
                alert("请先登录！");
            }

        }
    })
}

/**
 * 未登录不展示用户中心和购物车
 */
function  userChange() {
    $.ajax({
        "url":"../userlogin.do",
        "type":"POST",
        "data":{"method":"review"},
        "dataType":"json",
        "success":function(result){
            nameChange(result);
        }
    })
}
function nameChange(user) {
    if(user.name=='none'){
        $(".login").text("登录");
        $(".login").attr("href","../loginuser.html");
        $(".shopcar").attr("href","javascript:gotoshopcar();");
    }else {
        $(".login").text(" "+user.name+"");
        $(".login").attr("href","user.html?id="+user.id+"");
        $(".shopcar").attr("href","/xiaomi/shopcar.html")
    }
}
function gotoshopcar() {
    alert("请先登录，然后查看购物车！");
}